<template>
	<view class="login-page">
		<view class="login-top">
			<image src="/static/images/login-top-bg.png" mode="widthFix"></image>
			<text class="login-title">你好，欢迎进入健康管理</text>
		</view>
		<view class="login-tbody">
			<uv-tabs lineWidth="30" lineColor="#60bcbb" :activeStyle="{color: '#60bcbb',fontWeight: 'bold'}"
				:list="list" @click="tabClick" :scrollable="false"></uv-tabs>
			<view class="form">
				<view class="form-item">
					<uv-input placeholder="请输入手机号" border="none" v-model="phonenumber"></uv-input>
				</view>
				<view class="form-item">
					<uv-input placeholder="请输入短信验证码" border="none" v-model="smsCode">
						<template v-slot:suffix>
							<uv-code ref="uCode" @change="codeChange" seconds="20" changeText="X秒重新获取"></uv-code>
							<uv-button @click="getCode" :text="tips" type="success" size="mini"></uv-button>
						</template>
					</uv-input>
				</view>
			</view>
			<button class="button" @click="loginSubmit()">
				登 录
			</button>
			<view class="form-foot" v-if="isLoginType==2">
				<view @click="gotoRegister">注册</view>
			</view>
		</view>
		<view class="weixin" v-if="isLoginType==2">
			<text>微信快捷登录</text>
			<button class="wx-btn"  @click="getUserProfile">
				<image src="/static/images/weixin.png" mode=""></image>
			</button>
			
		</view>
	</view>
</template>

<script>
	import {
		nextTick
	} from 'vue';
	import {
		generateUUID
	} from '@/common/crypto/crypto.js';
	export default {
		data() {
			return {
				list: [{
					name: '用户登录',
				}, {
					name: '技师登录',
				}],
				phonenumber: null,
				tips: '',
				value: '内容',
				isLoginType: 2, //1技师 2客户
				tabIndex:0,
				formData: {
					tenantId: '000000',
					username: 'admin',
					password: 'admin123',
					rememberMe: false,
					// uuid: generateUUID(),
					uuid: 11,
					clientId: '428a8310cd442757ae699df5d894f051',
					grantType: 'password',
					code: '7',
				}
			}
		},
		mounted() {
			nextTick(() => {
				this.$store.dispatch('saveData', {
					field: 'isLoginType',
					value:this.isLoginType,
					keep: true,
				})
			})
		},
		methods: {
			// 微信授权
			getUserProfile(){
				uni.login({
					success: (res) => {
						console.log(res.code)
						this.wxLogin(res.code)
					},
				});
			},
			
			//微信授权code登陆
			async wxLogin(code){
				let par = {
					xcxCode:code,
					clientId: '428a8310cd442757ae699df5d894f051',
					tenantId: '000000',
					// phonenumber: this.phonenumber,
					// smsCode: this.smsCode,
					grantType: 'xcx',
					// isLoginType: this.isLoginType,
					isLoginType: 2,
					// clientId: '428a8310cd442757ae699df5d894f051',
					// tenantId: '000000',
				}
				let res = await this.muajax.login(par);
				this.$store.dispatch('saveData', {
					field: 'token',
					value: res.data.access_token,
					keep: true,
				})
				this.$store.dispatch('saveData', {
					field: 'loginInfor',
					value: res.data,
					keep: true,
				})
				uni.showToast({
					icon: 'success',
					title: '登录成功'
				})
				this.getInfo()
				
			},
			
			tabClick(e) {
				this.tabIndex= e.index
				this.isLoginType= this.tabIndex ? 1 : 2,
				this.$store.dispatch('saveData', {
					field: 'isLoginType',
					value: this.isLoginType,
					keep: true,
				})
				console.log(e)
			},
			codeChange(text) {
				this.tips = text;
			},
			// 获取短信验证码
			async getCode() {
				if (!this.phonenumber) {
					uni.showToast({
						icon: 'error',
						title: '请输入手机号'
					})
					return false
				}
				if (!this.$test.phone(this.phonenumber)) {
					this.toast({
						icon: 'error',
						title: '手机号格式不正确'
					});
					return false
				}
				if (this.$refs.uCode.canGetCode) {
					// get id请求用户信息方式
					// const res = await this.muajax.getJsUserId({
					// },{
					// 	urlRewrite:url=>`${url}/${this.id}`
					// })
					this.toast.loading({
						title: '正在获取验证码'
					})
					const res = await this.muajax.getsmsCode({
						phonenumber: this.phonenumber
					})
					if (res.code == 200) {
						uni.hideLoading()
						this.$refs.uCode.start();
						this.toast.alert({
							title: '验证码弹窗',
							content: res.msg
						})
						// this.toast('验证码已发送');
					}
				} else {
					this.toast('倒计时结束后再发送');
				}
			},
			gotoRegister() {
				uni.navigateTo({
					url: '/pages/register/register'
				})
			},
			// 登陆
			async loginSubmit() {
				console.log(this.formData)
				if (!this.phonenumber) {
					this.toast({
						icon: 'error',
						title: '请输入手机号'
					});
					return false
				}
				if (!this.$test.phone(this.phonenumber)) {
					this.toast({
						icon: 'error',
						title: '手机号格式不正确'
					});
					return false
				}
				if (!this.smsCode) {
					this.toast({
						icon: 'error',
						title: '请输入验证码'
					});
					return false
				}
				// let res = await this.muajax.login(this.formData);
				let par = {
					phonenumber: this.phonenumber,
					smsCode: this.smsCode,
					grantType: 'sms',
					isLoginType: this.isLoginType,
					// isLoginType: 1,
					clientId: '428a8310cd442757ae699df5d894f051',
					tenantId: '000000',
				}
				let res = await this.muajax.login(par);
				if (res.code == 200) {
					this.$store.dispatch('saveData', {
						field: 'token',
						value: res.data.access_token,
						keep: true,
					})
					this.$store.dispatch('saveData', {
						field: 'loginInfor',
						value: res.data,
						keep: true,
					})
					uni.showToast({
						icon: 'success',
						title: '登录成功'
					})
					this.getInfo()
				}

			},
			async getInfo() {
				let res = {}
				if (this.isLoginType == 1) {
					res = await this.muajax.getuserJsInfo()
				} else {
					res = await this.muajax.getCustomerInfo()
				}
				if (res.code == 200) {
					this.$store.dispatch('saveData', {
						field: 'userInfo',
						value: res.data,
						keep: true,
					})
					uni.switchTab({
						url: '/pages/home/home'
					})
				}

			}
		}
	}
</script>

<style scoped lang="less">
	page {
		height: 100%;
		overflow: hidden;
	}

	.login-top {
		width: 100%;
		height: 475rpx;
		position: fixed;
		top: 0px;

		image {
			width: 100%;

		}

		.login-title {
			position: absolute;
			top: 174rpx;
			left: 63rpx;
			font-size: 50rpx;
			font-weight: normal;
			font-stretch: normal;
			line-height: 46rpx;
			letter-spacing: 0rpx;
			color: #ffffff;
		}


	}

	.login-tbody {
		position: relative;
		z-index: 999;
		// top: -200rpx;
		margin: 0px auto;
		margin-top: 264rpx;
		width: 656rpx;
		// height: 812rpx;
		background-color: #ffffff;
		box-shadow: 0rpx 12rpx 10rpx 0rpx rgba(196, 196, 196, 0.26);
		border-radius: 20rpx;
		padding: 30rpx 0px;

	}

	.form {
		.form-item {
			width: 570rpx;
			height: 90rpx;
			background-color: #f1f1f1;
			border: 1px solid #f1f1f1;
			border-radius: 45rpx;
			margin: 50rpx auto;
			display: flex;
			align-items: center;
			padding: 1px 20rpx;
			box-sizing: border-box;
		}
	}

	.button {
		width: 567rpx;
		height: 96rpx;
		background: #00c6c3;
		// background: linear-gradient(-14deg,
		// 		#00c6c3 0%),
		// 	linear-gradient(#383738,
		// 		#383738);
		background-blend-mode: normal,
			normal;
		box-shadow: 0rpx 8rpx 10rpx 0rpx rgba(40, 9, 9, 0.1);
		border-radius: 48rpx;
		font-size: 42rpx;
		font-weight: normal;
		font-stretch: normal;
		letter-spacing: 2rpx;
		color: #ffffff;
		margin: 55rpx auto;

		&::after {
			border: 0px solid #fff;
		}
	}

	.form-foot {
		color: #999;
		text-align: center;
	}

	.weixin {
		margin-top: 54rpx;
		text-align: center;
		color: #999999;

		.wx-btn {
			margin: 24rpx auto;
			background: none;
			width: 67rpx;
			height: 67rpx;
			padding: 0px;

			image {
				width: 100%;
				height: 100%;
			}

			&::after {
				border: 0px solid #f1f1f1;

			}
		}
	}
</style>